import os
import decimal
import re
import sys
sys.path.append("/home/roosevelt/Desktop/bayes/trunk/modules/")
from BayesNetwork import BayesNetwork

out = "/home/roosevelt/Documents/resultados-bayes/outpath.csv"
path = "/home/roosevelt/Documents/resultados-bayes/"
main_dirs = os.listdir(path) 

BN = BayesNetwork()
BN.showTime = False
BN.showProgress = False

o = open(out, "w")
o.write("experiment, network, examples, perc, ini_structure, inverted, added, removed, likelihood_bn1, likelihood_bn2, bic_bn1, bic_bn2, cond_1, cond_2, count_class1, count_class2, aic_1, aic_2, time\n")

for folder in main_dirs:
    if os.path.isdir(path+folder):
        sub_dirs = os.listdir(path+folder)
        for sub_dir  in sub_dirs:
            if os.path.isdir(path+folder+"/"+sub_dir):
                exp_files = os.listdir(path+folder+"/"+sub_dir)
                # DATA and INI_STRUCTURE
                exp = sub_dir.strip().replace("perc", "").split("_")
                print exp
                if exp[1]=='alarm':
                    class_var = "VentLung"
                if exp[1]=='child':
                    class_var = "CO2"
                if exp[1]=='hailfinder':
                    class_var = "LLIW"
                if exp[1]=='insurance':
                    class_var = "SocioEcon"                                         
                
                for exp_file in exp_files:
                    if os.path.isfile(path+folder+"/"+sub_dir+"/"+exp_file):
                        if "ini_structure" in exp_file:
                            ini_structure = path+folder+"/"+sub_dir+"/"+exp_file
                        if "data.txt" in exp_file:
                            dataset = path+folder+"/"+sub_dir+"/"+exp_file
                            
                            sys.stdout = open(os.devnull, "w")
                            variables, data = BN.loadDataset(dataset)
                            sys.stdout = sys.__stdout__
                            size = decimal.Decimal(data.shape[0])
                            num_folds = decimal.Decimal("10")
                            n_per_fold = decimal.Decimal(size/num_folds).to_integral_exact(rounding=decimal.ROUND_FLOOR)
                            rest = size%num_folds

                            folds =[]
                            i = 0
                            while i < size:
                                in_fold = n_per_fold
                                if rest > 0:
                                    in_fold+=1
                                    rest-=1
                                folds.append(data[(i+1):i+in_fold+1])
                                i+=in_fold

                for exp_file in exp_files:                       
                    if os.path.isdir(path+folder+"/"+sub_dir+"/"+exp_file):
                        if exp_file.isdigit():
                            f_num = int(exp_file)
                            if os.path.isfile(path+folder+"/"+sub_dir+"/"+exp_file+"/output.txt"):
                                content = open(path+folder+"/"+sub_dir+"/"+exp_file+"/output.txt", "r").read()
                                text = re.search("Total time for experiment: [0-9\.]+", content)
                                time = decimal.Decimal(text.group(0).replace("Total time for experiment: ", ""))

                            
                            if os.path.isfile(path+folder+"/"+sub_dir+"/"+exp_file+"/final_structure"):
                                bn_1 = BN.parse_encog(ini_structure)
                                bn_2 = BN.parse_encog(path+folder+"/"+sub_dir+"/"+exp_file+"/final_structure")
                                test_data = folds[f_num]
                                sys.stdout = open(os.devnull, "w")
                                result = BN.compareBNStructures(bn_1, bn_2, variables, test_data, class_variable=class_var, classification_threshold=decimal.Decimal("0.5"))
                                sys.stdout = sys.__stdout__

                                
                            if os.path.isfile(path+folder+"/"+sub_dir+"/"+exp_file+"/alarm_final_structure"):
                                bn_1 = BN.parse_encog(ini_structure)
                                bn_2 = BN.parse_encog(path+folder+"/"+sub_dir+"/"+exp_file+"/alarm_final_structure")
                                test_data = folds[f_num]
                                sys.stdout = open(os.devnull, "w")
                                result = BN.compareBNStructures(bn_1, bn_2, variables, test_data, class_variable=class_var, classification_threshold=decimal.Decimal("0.5"))
                                sys.stdout = sys.__stdout__

                            result = exp+list(result)+[str(time)]
                            result = str(result).replace("[", "").replace("]","").replace("Decimal(","").replace(")", "").replace("'", "")
                            print result
                            o.write(result + "\n")
o.close()